<template>
  <div class="wrapper">
    <header>
      <p>待接订单</p>
    </header>

    <!-- 未接订单列表 -->
    <div class="order-list">
      <div v-for="order in orders" :key="order.orderId" class="order-item">
        <div class="order-info">
          <div class="order-id">订单号：{{ order.orderId }}</div>
          <div class="shop">商家地址：{{ order.bussinessname }}</div>
		  <div class="user">客户电话：{{ order.usert }}</div>
          <div class="address">送货地址：{{ order.deliveryAddress }}</div>
          <div class="amount">金额：¥{{ order.orderTotal }}</div>
        </div>
        <button class="accept-btn" @click="handleAccept(order.orderId)">接单</button>
      </div>

      <div v-if="orders.length === 0" class="empty">暂无待接订单</div>
    </div>

    <!-- 已接订单列表 -->
    <div class="order-list">
      <h2>已接订单</h2>
      <div v-for="order in acceptedOrders" :key="order.orderId" class="order-item">
        <div class="order-info">
          <div class="order-id">订单号：{{ order.orderId }}</div>
          <div class="shop">商家地址：{{ order.bussinessname }}</div>
		  <div class="user">客户电话：{{ order.usert }}</div>
          <div class="address">送货地址：{{ order.deliveryAddress }}</div>
          <div class="amount">金额：¥{{ order.orderTotal }}</div>
        </div>
      </div>

      <div v-if="acceptedOrders.length === 0" class="empty">暂无已接订单</div>
    </div>

    <Footer></Footer>
  </div>
</template>

<script>
import Footer from '../components/Footer.vue';

export default {
  data() {
    return {
      deliveryManId: this.$route.query.deliveryManId,
      orders: [],         // 未接订单列表
      acceptedOrders: []  // 已接订单列表
    };
  },
  mounted() {
    this.loadOrders();
    this.loadAcceptedOrders();
  },
  methods: {
	  
    // 加载未接订单
    async loadOrders() {
      try {
        let response = await this.$axios.get('/DeliveryMan/UndeliveryOrders');
        this.orders = response.data.result;

        // 获取每个订单的地址
        for (let order of this.orders) {
          order.deliveryAddress = await this.getAddress(order.daId);
		  order.bussinessname=await this.getname(order.orderId);
		  order.usert=await this.gettel(order.daId);
        }
      } catch (error) {
        console.error('加载未接订单失败:', error);
        alert('获取未接订单失败');
      }
    },

    // 加载已接订单
    async loadAcceptedOrders() {
      try {
        let response = await this.$axios.get('/DeliveryMan/OrderList', {
          params: { deliveryManId: this.deliveryManId }
        });
        this.acceptedOrders = response.data.result;

        // 获取每个订单的地址
        for (let order of this.acceptedOrders) {
          order.deliveryAddress = await this.getAddress(order.daId);
		  order.bussinessname=await this.getname(order.orderId);
		  order.usert=await this.gettel(order.daId);
        }
      } catch (error) {
        console.error('加载已接订单失败:', error);
        alert('获取已接订单失败');
      }
    },

    // 获取用户地址
    async getAddress(daId) {
      try {
        let response = await this.$axios.get('DeliveryAddress/DaId', {
          params: { daId }
        });
        return response.data.result.address || '未知地址';
      } catch (error) {
        console.error('获取地址失败:', error);
        return '地址加载失败';
      }
    },
	async gettel(daId) {
	  try {
	    let response = await this.$axios.get('DeliveryAddress/DaId', {
	      params: { daId }
	    });
	    return response.data.result.contactTel ;
	  } catch (error) {
	    console.error('获取地址失败:', error);
	    return '地址加载失败';
	  }
	},
	async getname(orderId) {
	  try {
	    let response = await this.$axios.get('Orders/OrdersId', {
	      params: { orderId }
	    });
	    return response.data.result.business.businessAddress || '未知地址';
	  } catch (error) {
	    console.error('获取地址失败:', error);
	    return '地址加载失败';
	  }
	},

    // 接单操作
    async handleAccept(orderId) {
      try {
        let url = `/DeliveryMan/deliveryOrder/${orderId}/${this.deliveryManId}`;
        let response = await this.$axios.put(url);

        if (response.data.result === 1) {
          alert('接单成功');
          this.orders = this.orders.filter(order => order.orderId !== orderId);
          this.loadAcceptedOrders();
        } else {
          alert('接单失败');
        }
      } catch (error) {
        console.error('接单失败:', error);
        alert('接单失败');
      }
    }
  },
  components: {
    Footer
  }
};
</script>

<style scoped>
.wrapper {
  padding-bottom: 14vw;
}

header {
  width: 100%;
  height: 12vw;
  background: #0097FF;
  color: white;
  font-size: 4.8vw;
  position: fixed;
  top: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.order-list {
  margin-top: 12vw;
  padding: 3vw;
}

.order-item {
  background: white;
  border-radius: 2vw;
  padding: 4vw;
  margin-bottom: 3vw;
  box-shadow: 0 1vw 2vw rgba(0,0,0,0.05);
}

.order-info div {
  margin-bottom: 2vw;
  font-size: 3.6vw;
}

.order-id {
  color: #666;
}

.shop {
  font-weight: bold;
  color: #333;
}

.address {
  color: #666;
}

.amount {
  color: #0097FF;
  font-weight: bold;
}

.accept-btn {
  width: 100%;
  height: 10vw;
  background: #0097FF;
  color: white;
  border: none;
  border-radius: 1vw;
  font-size: 4vw;
  margin-top: 4vw;
}

.empty {
  text-align: center;
  color: #999;
  font-size: 4vw;
  padding: 8vw 0;
}
</style>
